package top.lshaci.learning.csv;

import cn.hutool.core.codec.Base64;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVTest {

	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {

		String titles = "姓名,学号,年龄,";
		String keys = "name,no,age,";

		List<Map<String,Object>> dataList = new ArrayList<>();

		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("name", "张三");
		map1.put("no", "123");
		map1.put("age", "32");

		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("name", "李四");
		map2.put("no", "456");
		map2.put("age", "23");

		dataList.add(map1);
		dataList.add(map2);

		// 生成csv文件内容
		String exportMapCSV = CSVUtils.exportMapCSV(dataList, titles, keys);

		// 将csv内容写入byteArrayOutputStream
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		byteArrayOutputStream.write(exportMapCSV.getBytes());

		byte[] byteArray = byteArrayOutputStream.toByteArray();

		System.out.println("------------------------");
		System.out.println(byteArray);

		// base64加密
		String encode = Base64.encode(byteArray);

		byte[] decode = Base64.decode(encode.getBytes());

		// 输出
		File file = new File("E:\\out.csv");
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		fileOutputStream.write(decode);

	}
}
